package cn.wenlwan.server;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * creator: maokai
 * on: 2018/2/22
 * description:
 */
public class DiscardServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead (ChannelHandlerContext ctx , Object msg) throws Exception {
        try {
            String name = ctx.name();
            System.out.println( String.format( "接收到的消息--->: %s --- %s" , name , msg.toString() ) );
            ctx.write( msg );
            ctx.flush();
//            ByteBuf buf = (ByteBuf) msg;
//            while (buf.isReadable()) {
//                System.out.print( (char) buf.readByte() );
//                System.out.flush();
//            }
        } finally {
            // 默默地丢弃收到的数据
//            ReferenceCountUtil.release(msg);

            System.out.println( String.format( "接收消息完成" ) );

        }
    }

    @Override
    public void exceptionCaught (ChannelHandlerContext ctx , Throwable cause) throws Exception {
        try {
            System.out.println( "异常信息--->: " + cause.getMessage() );
        } finally {
            // 当出现异常就关闭连接
            cause.printStackTrace();
            ctx.close();
        }
    }
}
